const webpack = require('webpack')
const path = require('path')
const { name } = require('./package.json')
// const CopyWebpackPlugin = require('copy-webpack-plugin')
const { ContextReplacementPlugin } = require('webpack')

function resolve(dir) {
    return path.join(__dirname, dir)
}

module.exports = {
    publicPath: process.env.NODE_ENV === 'production' ? `/${name}/` : '/',

    // 开启生产环境SourceMap，设为false打包时不生成.map文件
    productionSourceMap: false,

    // 设置为 true 或 'warning' 时，eslint-loader 会将 lint 错误输出为编译警告。默认情况下，警告仅仅会被输出到命令行，且不会使得编译失败。
    lintOnSave: false, //去除eslint

    chainWebpack: config => {
        config.resolve.alias
            .set('@', resolve('src'))
            .set('src', resolve('src'))
            .set('config', resolve('src/config'))
            .set('mixin', resolve('src/mixin.js'))
            .set('api', resolve('src/api.js'))
            .set('route', resolve('src/route.js'))
            .set('filter', resolve('src/filter.js'))
            .set('view', resolve('src/views'))
            .set('@lib', resolve('src/lib'))
            .set('@assets', resolve('src/assets'))
            .set('@mixins', resolve('src/mixins'))
            .set('comp', resolve('src/components'))
            .set('@config', resolve('src/config'))
            .set('@layouts', resolve('src/layouts'))
            .set('@views', resolve('src/views'))
            .set('@router', resolve('src/router'))
            .set('@service', resolve('src/service'))
            .set('@store', resolve('src/store'))
            .set('style', resolve('src/styles'))
            .set('font', resolve('src/fonts'))
            .set('images', resolve('src/images'))
            .set('image', resolve('src/images'))
            .set('script', resolve('src/scripts'))
            .set('scripts', resolve('src/scripts'))
            .set('@utils', resolve('src/utils')) // alias config
        config.plugin('ignore').use(new ContextReplacementPlugin(/moment[/\\]locale$/, /zh-cn$/))
    },

    configureWebpack: config => {
        // 生产环境
        if (process.env.NODE_ENV === 'production') {
            // 生产环境去除console
            config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true

            // return {
            //   plugins: [new BundleAnalyzerPlugin()]
            // };
        }
        return {
            plugins: [
                new webpack.ProvidePlugin({
                    _: 'lodash',
                    moment: 'moment',
                }),
                // new CopyWebpackPlugin({
                //     patterns: [
                //         {
                //             from: 'src/images',
                //             to: 'images'
                //         },
                //         {
                //             from: 'src/scripts',
                //             to: 'scripts'
                //         }
                //     ]
                // })
            ],
        }
    },

    devServer: {
        // 设置让浏览器 overlay 同时显示警告和错误
        overlay: {
            warnings: false,
            errors: false,
        },
        proxy: {
            '/apis': {
                target: 'http://218.64.168.178:8000',
                changeOrigin: true,
                pathRewrite: {
                    '^/apis': '/',
                },
            },
        },
    },
    pages: {
        index: 'src/index.js',
    },
}
